<?php

if(isset($_SESSION['player'])){
	header('Location: index.php?p=home');	
}

if(isset($_POST['name'], $_POST['password'])){
	
	try {
		$_player = new Player();
		
		if($_player->isValidName($_POST['name'])){

			$_player->setName($_POST['name']);
			$_player->setPassword($_POST['password']);
			$_player->load();
		
			if($_player->getId()){
				
				$localisation = $_player->getLocalisation();
				$_SESSION['localisation'] = $localisation;
				setcookie('localisation', $localisation, 3600 * 24 * 30);
				$_SESSION['player'] = serialize($_player);

			}
			else {
				header('Location: index.php?p=login');	
			}
			//echo '<pre>'; print_r($_player); echo '</pre>';
		}
		header('Location: index.php?p=home');
	}
	catch(Exception $e){
		Log::addException($e);
		header('Location: index.php?p=login');
	}
}

?>